export default class TimeUtils {

    public static secondToMS(second: number) {
        if (second < 0) return '';
        let m = Math.floor(second / 60);
        let s = second % 60;
        let mString = m > 9 ? `${m}` : `0${m}`;
        let sString = s > 9 ? `${s}` : `0${s}`;
        return mString + ':' + sString;
    }

    public static secondToMSCN(second: number) {
        if (second < 0) return '';
        let m = Math.floor(second / 60);
        let s = second % 60;
        let mString = m > 0 ? `${m}分` : ``;
        let sString = s > 0 ? `${s}秒` : ``;
        return mString + sString;
    }

    public static secondToHMS(second: number) {
        if (second < 0) second = 0;
        let h = Math.floor(second / 3600).toString().padStart(2, "0");
        let m = Math.floor((second % 3600) / 60).toString().padStart(2, "0");
        let s = (second % 60).toString().padStart(2, "0");
        return h + ":" + m + ":" + s;
    }

    public static secondToHMSCN(second: number) {
        if (second < 0) second = 0;
        let str = ""
        let h = Math.floor(second / 3600);
        if (h > 0) str += `${h}时`;
        let m = Math.floor((second % 3600) / 60);
        if (m > 0) str += `${m}分`;
        let s = (second % 60);
        if (s > 0) str += `${s}秒`;
        return str;
    }
    
    public static secondToHM(second: number) {
        if (second < 0) second = 0;
        let h = Math.floor(second / 3600).toString().padStart(2, "0");
        let m = Math.floor((second % 3600) / 60).toString().padStart(2, "0");
        // let s = (second % 60).toString().padStart(2, "0");
        return h + ":" + m;
    }

    public static getTimeDifference(time1: Date, time2: Date) {
        return time2.getTime() - time1.getTime();
    }

    public static getDay(num: number, str: string) {
        let today = new Date();
        let nowTime = today.getTime();
        let ms = 24 * 3600 * 1000 * num;
        today.setTime(nowTime + ms);
        let oYear = today.getFullYear();
        let oMoth = (today.getMonth() + 1).toString();
        if (oMoth.length <= 1) oMoth = '0' + oMoth;
        let oDay = today.getDate().toString();
        if (oDay.length <= 1) oDay = '0' + oDay;
        return oYear + str + oMoth + str + oDay;
    }

    public static formatDate(time: number) {
        let date = new Date(time);
        let YY = date.getFullYear();
        let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
        let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
        return YY + '/' + MM + '/' + DD;
    }

    /**比较是否是当天 */
    public static compareIsToday(time: number): boolean {
        let src = new Date(time * 1000)
        let src_year = src.getFullYear()
        let src_mo = src.getUTCMonth()
        let src_day = src.getUTCDay()
        let cur = new Date(this.GetTimeBySecond() * 1000)
        let cur_year = cur.getFullYear()
        let cur_mo = cur.getUTCMonth()
        let cur_day = cur.getUTCDay()
        if (src_year == cur_year && src_mo == cur_mo && src_day == cur_day) {
            return true;
        }
        return false;
    }

    /**获取当前的秒数 */
    public static GetTimeBySecond(): number {
        let date = new Date;
        let datetime = Math.floor(date.getTime() / 1000);
        return datetime;
    }

    public static nowms() {
        return Date.now()
    }

    public static now() {
        return Number(Date.now() / 1000)
    }

    public static dateStamp() {
        return new Date(new Date().toLocaleDateString()).getTime() / 1000;
    }

    /**
     * @param time 单位：秒
     * @param cFormat 
     * @returns 
     */
    public static parseTime(time: number, cFormat?: string): string {
        if (arguments.length === 0) {
            return ""
        }
        const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
        let date = new Date(time < 1e11 ? time * 1000 : time)

        const formatObj = {
            y: date.getFullYear(),
            m: date.getMonth() + 1,
            d: date.getDate(),
            h: date.getHours(),
            i: date.getMinutes(),
            s: date.getSeconds(),
            a: date.getDay()
        }
        const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
            const value = formatObj[key as keyof typeof formatObj]
            // Note: getDay() returns 0 on Sunday
            if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
            return value < 10 ? '0' + value : value.toString()
        })
        return time_str
    }
    
    // 获取当天零点的时间戳
    public static getZeroTime(curTime: number) {
        let date = new Date(curTime);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    }

    // 获取当前距离1970年1月1日早上8点的总天数(即new Date(0)代表的时间)
    public static getTotalDay(curTime: number) {
        return Math.floor((curTime + 8 * 3600 * 1000) / 3600 / 1000 / 24)
    }
}